package cn.school.common;

import cn.school.entity.Grade;
import cn.school.service.GradeService;
import cn.school.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class GradeController {

    public String add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.getWriter().print("这是一个增加年级的方法！");
        String gname = request.getParameter("gname");
        //add
        return "redirect:grade-search.do";
        //return "/web-inf/addgrade.jsp";
    }

    public String del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.getWriter().print("这是一个del年级的方法！");
        return null;
    }

    public String update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.getWriter().print("这是一个update年级的方法！");
        return null;
    }

    public String search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.getWriter().print("这是一个search年级的方法！");
        return null;
    }

    public AjaxResult findALL(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        List<Grade> all = GradeService.INST.findALL();
        return AjaxResult.success(all);
//
//        StringBuilder sb = new StringBuilder();
//        sb.append("[");
//        for (int i = 0; i < all.size(); i++) {
//            Grade g = all.get(i);
//            sb.append("{");
//            sb.append("\"id\":" + g.getId() + ",");
//            sb.append("\"gname\":\"" + g.getGname() + "\"");
//            sb.append("}");
//            if (i < all.size() - 1)
//                sb.append(",");
//        }
//        sb.append("]");
//        //System.out.println(sb);
//        response.getWriter().print(sb);

    }
}
